package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.DTO.LoginDTO;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.controller.request.UserPageRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.service.IAdminService;
import com.example.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    //登录
    @PostMapping("/login")
    public Result save(@RequestBody LoginRequest request) {
        LoginDTO login = adminService.login(request);
        return Result.success(login);
    }

    //保存
    @PostMapping("/save")
    public Result login(@RequestBody Admin admin) {
        adminService.save(admin);
        return Result.success();
    }

    //更新
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return Result.success();
    }


    //获取
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Admin admins = adminService.getById(id);
        return Result.success(admins);
    }

    //更新密码
    @PutMapping("password")
    public Result password(@RequestBody PasswordRequest request){
        adminService.changePassword(request);
        return Result.success();
    }

    //单个删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        adminService.deleteByIds(ids);
        return Result.success();
    }

    //   查询全部
    @GetMapping("/list")
    public Result list() {
        List<Admin> users = adminService.list();
        return Result.success(users);
    }


    //    分页查询
    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest) {
        return Result.success(adminService.page(adminPageRequest));
    }


}
